using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class FuelController : MonoBehaviour
{
    public static FuelController instance;

    [SerializeField] private Image _fuelImage; // 显示燃料的图像
    [SerializeField, Range(0.1f, 5f)] private float _fuelDrainSpeed = 1f; // 燃料消耗速度范围
    [SerializeField] private float _maxFuelAmount = 100f; // 最大燃料量
    [SerializeField] private Gradient _fueleGradient;
    private float _currentFuelAmount; // 当前燃料量

    private void Awake()
    {
        if (instance == null)
            instance = this; // 单例模式，确保只有一个实例
    }

    private void Start()
    {
        _currentFuelAmount = _maxFuelAmount; // 初始时燃料量等于最大燃料量
        UpdateUI(); // 更新UI显示
    }

    private void Update()
    {
        _currentFuelAmount -= Time.deltaTime * _fuelDrainSpeed; // 每帧消耗燃料
        UpdateUI(); // 更新UI显示
        //燃料耗尽结束游戏
        if(_currentFuelAmount <= 0f){
            GameManager.instance.GameOver();
        }
    }

    private void UpdateUI()
    {
        _fuelImage.fillAmount = (_currentFuelAmount / _maxFuelAmount); // 更新燃料图像的填充量
        _fuelImage.color = _fueleGradient.Evaluate(_fuelImage.fillAmount);
    }

    //加油
    public void FillFuel(){
        _currentFuelAmount = _maxFuelAmount;
        UpdateUI();
    }
}